// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Bomb Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 41 : TYPE_BOMB

// Function declarations
reserve function Bomb_DebugDraw
reserve function Bomb_DebugSpawn

// Static Values

// Tables

function Bomb_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(8, FX_FLIP, object.xpos, object.ypos)
end function


function Bomb_DebugSpawn
	CreateTempObject(TypeName[Bomb], 0, object.xpos, object.ypos)
	object[tempObjectPos].value1 = object[tempObjectPos].xpos
	object[tempObjectPos].value2 = object[tempObjectPos].ypos
	object[tempObjectPos].xvel = -0x1000
	temp0 = object.direction
	object[tempObjectPos].direction = temp0
	temp0 &= 1
	if temp0 == 1
		FlipSign(object[tempObjectPos].xvel)
	end if
	object[tempObjectPos].value0 = 0x600
	object[tempObjectPos].animation = 0
	object[tempObjectPos].frame = 0
end function


event ObjectMain
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		object.value0--
		if object.value0 == 0
			object.value0 = 180
			object.animation = 1
			object.animationTimer = 0
			object.frame = 4
			object.state = 1
		else
			object.xpos += object.xvel
		end if
		break
	case 1
		object.value0--
		if object.value0 == 0
			object.value0 = 0x600
			object.animation = 0
			object.animationTimer = 0
			object.frame = 0
			FlipSign(object.xvel)
			object.direction ^= FLIP_X
			object.state = 0
		end if
		break
	case 2
		object.value3 += 0x1000
		object.value0--
		if object.value0 == 0
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, object.xpos, object.ypos)
			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = -0x20000
			object[tempObjectPos].yvel = -0x30000
			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = -0x10000
			object[tempObjectPos].yvel = -0x20000
			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = 0x20000
			object[tempObjectPos].yvel = -0x30000
			CreateTempObject(TypeName[Bomb Shrapnel], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = 0x10000
			object[tempObjectPos].yvel = -0x20000
			PlaySfx(SfxName[Explosion], 0)
		end if
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value1
		object.ypos = object.value2
		if object.outOfBounds == 1
			object.xvel = -0x1000
			temp0 = object.propertyValue
			object.direction = temp0
			temp0 &= 1
			if temp0 == 1
				FlipSign(object.xvel)
			end if
			object.value0 = 0x600
			object.animation = 0
			object.frame = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state != 2
			BoxCollisionTest(C_TOUCH, object.entityPos, -96, -96, 96, 96, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.animation = 2
				object.frame = 6
				object.value0 = 144
				object.value3 = 0
				object.state = 2
			end if
		end if
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			CallFunction(PlayerObject_Hit)
		end if		
	next

	foreach (TypeName[Bullet], arrayPos0, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -18, 12, 18, arrayPos0, -6, -4, 6, 4)
		if checkResult == 1
			object.animation = 2
			object.frame = 6
			object.value0 = 1
			object.value3 = 0
			object.state = 2
		end if
	next	
	
	switch object.animation
	case 0
		if object.animationTimer == 20
			object.animationTimer = 0
			object.frame++
			object.frame &= 3
		end if
		object.animationTimer++
		break
	case 1
	case 2
		if object.animationTimer == 20
			object.animationTimer = 0
			object.frame ^= 1
		end if
		object.animationTimer++
		break
	end switch
end event


event ObjectDraw
	if object.state == 2
		temp0 = object.value0
		temp0 >>= 2
		temp0 &= 1
		temp0 += 8
		temp1 = object.ypos
		GetBit(temp2, object.direction, 1)
		if temp2 == 0
			temp1 += object.value3
		else
			temp1 -= object.value3
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, temp1)
	end if
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone05")
	if checkResult == 1
		LoadSpriteSheet("SLZ/Objects.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 131)
		SpriteFrame(-10, -22, 20, 38, 28, 131)
		SpriteFrame(-10, -21, 20, 37, 3, 131)
		SpriteFrame(-10, -22, 20, 38, 53, 92)
		SpriteFrame(-11, -21, 23, 37, 2, 92)
		SpriteFrame(-11, -21, 23, 37, 27, 92)
		SpriteFrame(-11, -9, 23, 25, 2, 170)
		SpriteFrame(-11, -9, 23, 25, 27, 170)
		SpriteFrame(-3, -23, 6, 14, 51, 170)
		SpriteFrame(-4, -25, 8, 16, 58, 170)
	end if
	CheckCurrentStageFolder("Zone06")
	if checkResult == 1
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-10, -21, 20, 37, 52, 40)
		SpriteFrame(-10, -22, 20, 38, 27, 40)
		SpriteFrame(-10, -21, 20, 37, 2, 40)
		SpriteFrame(-10, -22, 20, 38, 52, 1)
		SpriteFrame(-11, -21, 23, 37, 1, 1)
		SpriteFrame(-11, -21, 23, 37, 26, 1)
		SpriteFrame(-11, -9, 23, 25, 1, 79)
		SpriteFrame(-11, -9, 23, 25, 26, 79)
		SpriteFrame(-3, -23, 6, 14, 50, 79)
		SpriteFrame(-4, -25, 8, 16, 57, 79)
	end if
	CheckCurrentStageFolder("BossRush")
	if checkResult == 1
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 328)
		SpriteFrame(-10, -22, 20, 38, 28, 328)
		SpriteFrame(-10, -21, 20, 37, 3, 328)
		SpriteFrame(-10, -22, 20, 38, 53, 289)
		SpriteFrame(-11, -21, 23, 37, 2, 289)
		SpriteFrame(-11, -21, 23, 37, 27, 289)
		SpriteFrame(-11, -9, 23, 25, 2, 367)
		SpriteFrame(-11, -9, 23, 25, 27, 367)
		SpriteFrame(-3, -23, 6, 14, 51, 367)
		SpriteFrame(-4, -25, 8, 16, 58, 367)
	end if
	CheckCurrentStageFolder("Zone00")
	if checkResult == 1
		LoadSpriteSheet("GHZ/Objects3.gif")
		SpriteFrame(-10, -21, 20, 37, 53, 328)
		SpriteFrame(-10, -22, 20, 38, 28, 328)
		SpriteFrame(-10, -21, 20, 37, 3, 328)
		SpriteFrame(-10, -22, 20, 38, 53, 289)
		SpriteFrame(-11, -21, 23, 37, 2, 289)
		SpriteFrame(-11, -21, 23, 37, 27, 289)
		SpriteFrame(-11, -9, 23, 25, 2, 367)
		SpriteFrame(-11, -9, 23, 25, 27, 367)
		SpriteFrame(-3, -23, 6, 14, 51, 367)
		SpriteFrame(-4, -25, 8, 16, 58, 367)
	end if
	foreach (TypeName[Bomb], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].value2 = object[arrayPos0].ypos
		object[arrayPos0].xvel = -0x1000
		temp0 = object[arrayPos0].propertyValue
		object[arrayPos0].direction = temp0
		temp0 &= 1
		if temp0 == 1
			FlipSign(object[arrayPos0].xvel)
		end if
		object[arrayPos0].value0 = 0x600
		object[arrayPos0].animation = 0
		object[arrayPos0].frame = 0
	next
	SetTableValue(TypeName[Bomb], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Bomb_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Bomb_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
